package k3_IO.p1_file.demo05;

public class BinarySearch {

    public static int search(int[] nums,int target,int startIndex,int endIndex){
        //控制起始位置与终止位置的合理性，这里还有很多条件需要判断，暂时忽略
        if(startIndex>endIndex){
            return -1;
        }

        //找出中间的索引位置
        int middleNum = (startIndex+endIndex) >>1;

        if(nums[middleNum]==target){
            return middleNum;
        }else if(nums[middleNum]<target){
            return search(nums,target,middleNum,endIndex);
        }else{
            return search(nums,target,startIndex,middleNum);
        }
    }


    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(BinarySearch.search(nums, 8, 0, nums.length - 1));
    }

}
